<template>
  <div class="app-container">
    <el-form
      label-position="left"
      label-width="120px"
      ref="passwordForm"
      :model="passwordForm"
      :rules="rules"
    >
      <el-row>
        <el-col :span="15">
          <el-form-item label="当前账号：" prop="username">
            <el-input v-model="passwordForm.username" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="原密码：" prop="password">
            <el-input v-model="passwordForm.password" show-password />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="新密码：" prop="newPassword">
            <el-input v-model="passwordForm.newPassword" show-password />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="确认密码：" prop="checkPassword">
            <el-input v-model="passwordForm.checkPassword" show-password />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(passwordForm)">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updatePwd } from "@/api/user";
export default {
  data() {
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.passwordForm.checkPassword !== "") {
          this.$refs.passwordForm.validateField("checkPassword");
        }
        callback();
      }
    };
    var validatePassword2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.passwordForm.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      passwordForm: {
        username: "",
        password: "",
        newPassword: "",
        checkPassword: ""
      },
      rules: {
        oldAccount: {
          password: [
            { required: true, message: "请输入密码", trigger: "blur" },
            {
              min: 6,
              max: 15,
              message: "长度在 6 到 15 个字符",
              trigger: "blur"
            }
          ]
        },
        newPassword: [
          { validator: validatePassword, trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ],
        checkPassword: [{ validator: validatePassword2, trigger: "blur" }]
      }
    };
  },
  methods: {
    onSubmit(passwordForm) {
      updatePwd({
        oldPassword: passwordForm.password,
        newPassword: passwordForm.newPassword
      }).then(response => {
          this.$message({
            message: response.message,
            type: "success"
          });
      })
      this.$refs.passwordForm.resetFields();
    }
  },
  created() {
    this.passwordForm.username = this.$store.getters.name;
  }
};
</script>

<style lang='scss' scoped>
.line {
  text-align: center;
}
</style>

